<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="Document Id" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-col :span="20">
                <a-input v-model="model.id" placeholder="请输入id"></a-input>
              </a-col>
              <a-col :span="4">
                <a-button @click="handleCreate" type="link">自动生成</a-button>
              </a-col>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-tabs v-model="activeId">
          <a-tab-pane key="1" tab="JOSN Input">
            <vue-json-editor v-model="model" :showBtns="false" :mode="'code'" @json-change="handleInputJosnChange" />
          </a-tab-pane>
          <a-tab-pane key="2" tab="Form View">
            <dynamicForm
              ref="dynamicForm"
              :fieldOptions="fieldOptions"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              :model="model"
            ></dynamicForm>
          </a-tab-pane>
        </a-tabs>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>
  
  <script>
import vueJsonEditor from 'vue-json-editor'
import store from '@/store/'
import dynamicForm from './dynamicForm'
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { v4 as uuidv4 } from 'uuid'
const mappingRe = {
  text: 'text',
  float: 'text',
  date: 'datePicker',
  long: 'text',
  boolean: 'select',
  integer: 'text',
  keyword: 'text',
  object: 'inputJOSN',
}
export default {
  name: 'ChatVectorFileCabinetForm',
  components: {
    vueJsonEditor,
    dynamicForm,
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {},
      activeId: '1',
      docIndex: '',
      configId: '',
      labelCol: {
        xs: { span: 24 },
        sm: { span: 4 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 18 },
      },
      fieldOptions: [],
      confirmLoading: false,
      validatorRules: {
        cabinetName: [{ required: true, message: '请输入文件柜名称' }],
      },

      url: {
        add: '/es/save',
        edit: '/es/save',
        queryById: '/ChatVectorFileCabinet/chatVectorFileCabinet/queryById',
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add(index, mappings) {
      console.log(mappings)
      let arr = []
      for (let key in mappings) {
        if (key != 'id') {
          const obj = {
            label: key,
            key: key,
            value: null,
            type: this.returnType(mappings[key].type ? mappings[key].type : 'object'),
            required: false,
            props: {
              valueFormat: mappings[key].type == 'date' ? 'YYYY-MM-DD' : null,
            },

            options:
              mappings[key].type == 'boolean'
                ? [
                    { label: 'true', value: true },
                    { label: 'false', value: false },
                  ]
                : null,
          }
          arr.push(obj)
        }

        this.fieldOptions = arr
      }
      let record = {}
      this.configId = index
      this.edit(record)
    },
    handleCreate() {
      console.log(uuidv4())

      this.model.id = uuidv4()
      this.$forceUpdate()
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    returnType(type) {
      return mappingRe[type]
    },
    handleInputJosnChange(e) {
      console.log(e)
    },
    submitForm() {
      this.model.configId = this.configId
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'post'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>
